const express = require('express');
const router = express.Router()
const { ttPay } = require('../ttpay');
const app = express();
const port = 3000;
const bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({
	extend: false
}));
app.use(bodyParser.json());
app.use(router);
app.post('/createOrder', async (req, res) => {
	console.log('参数', req.body)
	let newObj = req.body;
	let cashdeskParams = '';
	if (newObj.type == 1) {
		cashdeskParams = await ttPay({
			appId: 'appId',
			merchantId: 'merchantId',
			alipayUrl: newObj.alipayUrl,
			outOrderNo: newObj.outOrderNo, // 商户侧订单号
			timestamp: newObj.timestamp, // 时间戳，秒
			totalAmount: newObj.totalAmount, // 金额，单位为分
			uid: newObj.uid, // 用户标识
			subject: newObj.subject, //商户订单名称
			body: newObj.body, //商户订单详情
			tradeTime: newObj.tradeTime,
			notifyUrl: newObj.notifyUrl,
			validTime: newObj.validTime,
			appSecret: 'appSecret',
			appletVersion: '2.0+',
		});
	} else {
		cashdeskParams = await ttPay({
			appId: 'appId',
			merchantId: 'merchantId',
			wxUrl: newObj.wxUrl,
			outOrderNo: newObj.outOrderNo, // 商户侧订单号
			timestamp: newObj.timestamp, // 时间戳，秒
			totalAmount: newObj.totalAmount, // 金额，单位为分
			uid: newObj.uid, // 用户标识
			subject: newObj.subject, //商户订单名称
			body: newObj.body, //商户订单详情
			tradeTime: newObj.tradeTime,
			notifyUrl: newObj.notifyUrl,
			validTime: newObj.validTime,
			appSecret: 'appSecret',
			appletVersion: '2.0+',
		});
	}
	res.json(cashdeskParams);
});

app.listen(port, () => console.log(`Example app listening on port ${port}!`));
